/**
 * 
 */
package com.res.simulator.transformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.res.common.transformer.BaseTransformer;
import com.res.simulator.model.GenericAPIExecutionModel;
import com.res.simulator.view.ConsumerTemplateSearchResultsView;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.SystemInfo;
import com.sip.dbadapter.domain.registry.DomainRegistry;

/**
 * @author dl40288
 */
public class APISearchTransformer implements BaseTransformer<GenericAPIExecutionModel, List<ConsumerTemplateSearchResultsView>, Map<String, Object>> {

    public Map<String, Object> transformModelToDomian(final GenericAPIExecutionModel model) throws Exception {

        final ApiDefinition apiDefinition = new ApiDefinition();
        final SystemInfo systemInfo = new SystemInfo();

        apiDefinition.setName(model.getApiName());
        apiDefinition.setPackageHierachy(model.getPackageName());

        systemInfo.setSystemName(model.getSystemName());

        apiDefinition.setSystemInfo(systemInfo);

        final Map<String, Object> domainCollection = new HashMap<String, Object>();
        domainCollection.put(DomainRegistry.API_DEFINITION, apiDefinition);
        domainCollection.put(DomainRegistry.SYSTEM_INFO, systemInfo);

        return domainCollection;

    }

    public List<ConsumerTemplateSearchResultsView> transformDomianToView(Map<String, Object> apiDomains) throws Exception {

        final List<ConsumerTemplateSearchResultsView> apiSearchResultsViewList = new ArrayList<ConsumerTemplateSearchResultsView>();

        final Set<String> keySet = apiDomains.keySet();
        for (final String key : keySet) {

            final ConsumerTemplateSearchResultsView apiSearchResultsView = new ConsumerTemplateSearchResultsView();
            final ApiDefinition apiDefinition = (ApiDefinition) apiDomains.get(key);

            apiSearchResultsView.setApiId(Long.toString(apiDefinition.getApiId()));
            apiSearchResultsView.setApiName(apiDefinition.getName());
            apiSearchResultsView.setPackageStructure(apiDefinition.getPackageHierachy());

            apiSearchResultsViewList.add(apiSearchResultsView);
        }

        return apiSearchResultsViewList;
    }

}
